#!/bin/bash
#========================================================================
# Author: YSLin
# Email: 
# File Name: removeSpaceFile.sh
# Description: 
#
# this shell script removes the spaces out of the name of every file argument
# passed to it (handles an arbitrary number of files on the command line).
# example:
# removes.sh "/Users/Kurt/Name With Spaces"
# results in changing the filename to "/Users/Kurt/NameWithSpaces"
   
# Edit History: 
#   2011-02-16    File created.
#========================================================================

OriginalFile="/Users/Kurt/Name With Spaces"

Location=`dirname "$OriginalFile"`
FileName=`basename "$OriginalFile"`

ShortName=`echo $FileName | sed 's/ //g'`

if [ $ShortName != "$FileName" ]
then
	#		cd "$Location"
	#		mv "$FileName" "$ShortName"
	echo $FileName;
	echo $ShortName;
fi

#Here are couple of one liners that will help fix this by removing one or more spaces in the file names.

#In bash shell:

#replace spaces with underscores in the file names
for file in *\ *; do mv "$file" "${file// /_}"; done
#remove all spaces in the file names
for file in *\ *; do mv "$file" "${file// /}"; done

#The following will work in both k-shell and bash shell:

#replace spaces with underscores in the file names
for file in *\ *; do target="$(echo $file | sed -e 's/ /_/g')"; mv "$file" "$target";done;

#remove all spaces in the file names
for file in *\ *; do target="$(echo $file | sed -e 's/ //g')"; mv "$file" "$target";done;
